package com.satan.fmmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.satan.fmmall.entity.ShoppingCart;
import com.satan.fmmall.mapper.ShoppingCartMapper;
import com.satan.fmmall.service.ShoppingCartService;
import com.satan.fmmall.vo.ResCode;
import com.satan.fmmall.vo.ResultVo;
import com.satan.fmmall.vo.ShoppingCartVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName ShoppingCartServiceImpl
 * @Description TODO
 * @date 2022/4/12 21:13
 * @Version 1.0
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;


    @Override
    public ResultVo addShoppingCart(ShoppingCart shoppingCart) {
        int insert = shoppingCartMapper.insert(shoppingCart);
        if (insert == 1) {
            return ResultVo.success(null);
        } else {
            return ResultVo.fail(ResCode.INSERT_ERROR);
        }
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public ResultVo listShoppingCartsByUserId(Integer userId) {
        return ResultVo.success(shoppingCartMapper.selectShopCartsByUserId(userId));
    }

    @Override
    public ResultVo updateCartNum(Integer cartId, Integer cartNum) {
        LambdaUpdateWrapper<ShoppingCart> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ShoppingCart::getCartId, cartId);
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setCartNum(cartNum + "");
        int i = shoppingCartMapper.update(shoppingCart, updateWrapper);
        if (i >0) {
            return ResultVo.success(null);
        }else {
            return ResultVo.fail(ResCode.UPDATE_ERROR);
        }

    }

    @Override
    public ResultVo deleteShopCartByCartId(Integer[] cartIds) {
        int i = shoppingCartMapper.deleteBatchIds(Arrays.asList(cartIds));
        if (i > 0) {
            return ResultVo.success(null);
        }else {
            return ResultVo.fail(ResCode.DELETE_ERROR);
        }
    }

    @Override
    public ResultVo listShoppingCartsByCartIds(Integer[] cartIds) {
        List<ShoppingCartVo> shoppingCartVoList = shoppingCartMapper.selectShopCartsByCartIds(Arrays.asList(cartIds));
        return ResultVo.success(shoppingCartVoList);
    }


}
